1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.util.concurrent.testing;
18
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.util.concurrent.ListeningScheduledExecutorService;
21
22 import junit.framework.TestCase;
23
24 import java.lang.InterruptedException;
25 import java.util.List;
26 import java.util.concurrent.Callable;
27 import java.util.concurrent.CancellationException;
28 import java.util.concurrent.ExecutionException;
29 import java.util.concurrent.Future;
30 import java.util.concurrent.ScheduledFuture;
31 import java.util.concurrent.TimeUnit;
32
33
34
35
36
37
38 public class TestingExecutorsTest extends TestCase {
39 private volatile boolean taskDone;
40
41 public void testNoOpScheduledExecutor() throws InterruptedException {
42 taskDone = false;
43 Runnable task = new Runnable() {
44 @Override public void run() {
45 taskDone = true;
46 }
47 };
48 ScheduledFuture<?> future = TestingExecutors.noOpScheduledExecutor().schedule(
49 task, 10, TimeUnit.MILLISECONDS);
50 Thread.sleep(20);
51 assertFalse(taskDone);
52 assertFalse(future.isDone());
53 }
54
55 public void testNoOpScheduledExecutorShutdown() {
56 ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
57 assertFalse(executor.isShutdown());
58 assertFalse(executor.isTerminated());
59 executor.shutdown();
60 assertTrue(executor.isShutdown());
61 assertTrue(executor.isTerminated());
62 }
63
64 public void testNoOpScheduledExecutorInvokeAll() throws ExecutionException, InterruptedException {
65 ListeningScheduledExecutorService executor = TestingExecutors.noOpScheduledExecutor();
66 taskDone = false;
67 Callable<Boolean> task = new Callable<Boolean>() {
68 @Override public Boolean call() {
69 taskDone = true;
70 return taskDone;
71 }
72 };
73 List<Future<Boolean>> futureList = executor.invokeAll(
74 ImmutableList.of(task), 10, TimeUnit.MILLISECONDS);
75 Future<Boolean> future = futureList.get(0);
76 assertFalse(taskDone);
77 assertTrue(future.isDone());
78 try {
79 future.get();
80 fail();
81 } catch (CancellationException e) {
82
83 }
84 }
85
86 public void testSameThreadScheduledExecutor() throws ExecutionException, InterruptedException {
87 taskDone = false;
88 Callable<Integer> task = new Callable<Integer>() {
89 @Override public Integer call() {
90 taskDone = true;
91 return 6;
92 }
93 };
94 Future<Integer> future = TestingExecutors.sameThreadScheduledExecutor().schedule(
95 task, 10000, TimeUnit.MILLISECONDS);
96 assertTrue("Should run callable immediately", taskDone);
97 assertEquals(6, (int) future.get());
98 }
99
100 public void testSameThreadScheduledExecutorWithException() throws InterruptedException {
101 Runnable runnable = new Runnable() {
102 @Override public void run() {
103 throw new RuntimeException("Oh no!");
104 }
105 };
106
107 Future<?> future = TestingExecutors.sameThreadScheduledExecutor().submit(runnable);
108 try {
109 future.get();
110 fail("Should have thrown exception");
111 } catch (ExecutionException e) {
112
113 }
114 }
115 }